﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TracerMonitor : MonoBehaviour
{
    private static TracerMonitor instance = null;
    
    [RuntimeInitializeOnLoadMethod]
    private static void CheckInstance()
    {
        if (instance == null && Application.isPlaying)
        {
            var go = new GameObject("TracerMonitor");
            GameObject.DontDestroyOnLoad(go);
            instance = go.AddComponent<TracerMonitor>();
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Tracer.Trace("start");
        StartCoroutine(Tracer.Send());
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.frameCount % 900 == 0)
        {
            StartCoroutine(Tracer.Send());
        }
    }

    private void OnApplicationPause(bool pauseStatus)
    {
        Tracer.Trace(pauseStatus ? "pause": "resume");
        StartCoroutine(Tracer.Send());
    }

    private void OnApplicationQuit()
    {
        // TODO: right ?
        Tracer.Trace("quit");
        Tracer.SaveToLocalStorage();
    }
}
